package com.misyi.util;

import com.misyi.leetcode.editor.cn.ListNode;

/**
 * 链表工具包
 *
 * @author licong
 * @date 2021-10-19 下午4:02
 */
public class ListNodeUtils {

    /**
     * 创建一个链表
     *
     * @param size 链表数量
     * @return
     */
    public static ListNode create(int size) {
        ListNode head = null;
        for (int i = size; i > 0; i--) {
            head = new ListNode(i, head);
        }
        return head;
    }

    /**
     * 打印链表数据
     */
    public static void print(ListNode node) {
        StringBuilder sb = new StringBuilder(256);
        sb.append("[");
        boolean flag = false;
        while (node != null) {
            if (flag) {
                sb.append(",");
            }
            flag = true;
            sb.append(node.val);
            node = node.next;
        }
        sb.append("]");
        System.out.println(sb.toString());
    }
}
